//
//  TurntableCollectionViewLayout.m
//  MyProject
//
//  Created by yun on 2018/7/26.
//  Copyright © 2018年 yun. All rights reserved.
//


#import "TurntableCollectionViewLayout.h"


@implementation TurntableUICollectionViewLayoutAttributes

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup{
    self.angle = 0;
    self.anchorPoint = CGPointMake(0.5, 0.5);
}

- (void)setAngle:(CGFloat)angle{
    _angle = angle;
    // 转动
    self.transform = CGAffineTransformMakeRotation(angle);
}

// 必须重写，不然angle和anchorPoint不会变
- (id)copyWithZone:(NSZone *)zone{
    TurntableUICollectionViewLayoutAttributes *attr = [super copyWithZone:zone];
    attr.angle = self.angle;
    attr.anchorPoint = self.anchorPoint;
    return attr;
}

@end


@interface TurntableCollectionViewLayout ()

@property(nonatomic,assign) CGSize itemSize;
@property(nonatomic,assign) CGFloat radius;// 半径
@property(nonatomic,assign) CGFloat itemAngle;// 各个item角度
@property(nonatomic,strong) NSMutableArray *attributesArray;// attributes数组


@end

@implementation TurntableCollectionViewLayout

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initSet];
    }
    return self;
}

- (void)initSet{
    self.itemSize = CGSizeMake(76, 82);// 适配图片
    self.radius = 200;
    self.attributesArray = [NSMutableArray array];
}

+ (Class)layoutAttributesClass{
    return [TurntableUICollectionViewLayoutAttributes class];
}

// 整个content的size,先取collectionView的size
- (CGSize)collectionViewContentSize{
    
    return CGSizeMake(self.itemSize.width * [self.collectionView numberOfItemsInSection:0], self.collectionView.bounds.size.height);
}

// 滚动时使其布局无效，这样的话这样计算attr.angle = self.itemAngle * i无效，我们可以根据偏移量来计算
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}

// 开始布局
- (void)prepareLayout{
    [super prepareLayout];
    // 每次布局都删除之前的attribute
    [self.attributesArray removeAllObjects];
    // item居中
    CGFloat centerX = self.collectionView.contentOffset.x + CGRectGetWidth(self.collectionView.bounds)/2.f;
    CGFloat anchorPointY = (self.itemSize.height + self.radius)/self.itemSize.height;
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (NSInteger i = 0; i < count; i++) {
        TurntableUICollectionViewLayoutAttributes *attr = [TurntableUICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        attr.size = self.itemSize;
        attr.center = CGPointMake(centerX, CGRectGetMidY(self.collectionView.bounds));
        attr.angle = self.itemAngle * i - self.collectionView.contentOffset.x/(self.collectionViewContentSize.width - CGRectGetWidth(self.collectionView.bounds));
        attr.anchorPoint = CGPointMake(0.5, anchorPointY);
        [self.attributesArray addObject:attr];
    }
    
}

- (nullable NSArray<__kindof TurntableUICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    return self.attributesArray;
}

#pragma mark -- getter
// 自己设定的旋转角度
- (CGFloat)itemAngle{
    return M_PI/18.f;
}

#pragma mark -- setter

- (void)setRadius:(CGFloat)radius{
    _radius = radius;
    [self invalidateLayout];// 使当前布局无效并触发布局更新。
}


@end
